home *** CD-ROM | disk | FTP | other *** search
- Path: news.iag.net!news
- From: jatmon@iag.net (John R Buchan)
- Newsgroups: comp.lang.c
- Subject: Re: Please help ?!
- Date: 20 Jan 1996 21:48:17 GMT
- Organization: Internet Access Group, Orlando, Florida
- Message-ID: <4drnv1$cr@news.iag.net>
- References: <4dm889$3hs@neptunus.pi.net>
- NNTP-Posting-Host: pm1-orl30.iag.net
- X-Newsreader: WinVN 0.99.7
-
- In article <4dm889$3hs@neptunus.pi.net>, mv@pi.net says...
- >
- >Hello everybody,
- >
- >
- >Please take a look at this:
- <code snipped>
-
- Below is a modified version of the posted code. All comments are mine
- <hint about need for comments> and usually refer to modifications I have made.
-
- The original problem appears to have been a side effect of the incorrect
- definition of the second parameter in get_switches. The c.l.c faq (Frequently
- Asked Question) list explains this in more detail. It is available for
- anonymous ftp from rtfm.mit.edu /pub/usenet/comp.lang.c.
-
-
- #include <stdio.h>
- #include <stdlib.h>
- /* #include <conio.h> not ansi */
- #include <string.h>
- /* #include <alloc.h> not ansi or needed (malloc is in stdlib.h) */
-
-
- #define MAXLEN 20
- #define MAXSWITCH 20
-
- /* You should normally avoid using globals unless you have a specific */
- /* need for them. They tend to make code hard to read. */
- char *s = "dir /w:test/1/2/3/4";
- char *s2, *s3;
- char myswitches[MAXSWITCH][MAXLEN];
-
-
- /* int get_switches(char *cmd, char *switches[][MAXLEN]) */
- /* this is a pointer to a pointer to an array of MAXLEN chars. your */
- /* compiler didn't generated numerous warnings as a result? try */
- /* int get_switches(char *cmd, char switches[][MAXLEN]) or */
- /* int get_switches(char *cmd, char *switches[MAXLEN]) or */
- int get_switches(char *cmd, char switches[MAXSWITCH][MAXLEN])
- {
- int i = 0;
- char *cpy;
- char *saved;
-
- if (strlen(cmd) == 0)
- {
- return 0;
- }
-
- /* Why malloc? You could just define saved as char saved[MAXLEN]; */
- /* and set cpy = saved */
- cpy = (char *) malloc(MAXLEN);
- if (cpy == NULL)
- {
- printf("Out of memory in %s on line: %d\n", __FILE__, __LINE__);
- exit(EXIT_FAILURE);
- }
-
- /* strcpy(cpy, '\0'); Your compiler should have complained */
- /* '\0' is a char, not the requires char pointer. Try */
- cpy[0] = '\0';
- saved = cpy;
- while (*cmd != '\0')
- {
-
- /* if (*cmd == '/') modified to avoid duplicate code */
- if ( (*cmd == '/') || (*cmd == '-') )
- {
- if (i == MAXSWITCH) /* test switch limit */
- {
- printf("INT: Too many switches ...\n");
- return 9999; /* wouldn't it be easier to return a negative? */
- }
-
- *cpy = *cmd; /* parse switch to cpy (saved) */
- do
- {
- *cpy++ = *cmd++;
- }
- while (!strchr(" /-\0", *cmd));
- *cpy = '\0';
- cpy = saved;
-
- strcpy(switches[i], cpy); /* copy switch to switches array */
- cpy[0] = '\0'; /* strcpy(cpy, '\0'); */
- i++;
- }
-
- /* this is duplicate code. just add the condition to the prior if
- if (*cmd == '-')
- {
-
- if (i == MAXSWITCH)
- {
- printf("INT: Too many switches ...\n");
- return 9999;
- }
-
- *cpy = *cmd;
- do
- {
- *cpy++ = *cmd++;
- }
- while (!strchr(" /-\0", *cmd));
- *cpy = '\0';
- cpy = saved;
-
- strcpy(switches[i], cpy);
- strcpy(cpy, '\0');
- i++;
- }
- end duplicate code */
-
- /* if (!strchr("/-\0", *cmd)) make this an else. no conditions needed */
- else
- {
- *cmd++;
- }
- }
- free(cpy);
- return i;
- }
-
-
- int main(void)
- {
- int i, j;
- /* clrscr(); not ansi */
- i = get_switches(s, myswitches);
- printf("Switches found: %i\n", i);
-
- /* for (i = 0; i < MAXSWITCH; i++) why print blank switches? */
- for (j = 0; j < i; j++)
- {
- printf("Switch: %s\n", myswitches[j]);
- }
- return 0;
- }
-
-
- --
- John R Buchan -:|:- Looking for that elusive FAQ? ftp to:
- jatmon@mail.iag.net -:|:- rtfm.mit.edu /pub/usenet-by-group/....
-
-